<template>
    <div class="container">
        <div class="comment" v-for="item in comments">
            <div class="info">
                <img class="avatar" :src="item.avatar" width="36" height="36"/>
                <div class="right">
                    <div class="name">{{item.name}}</div>
                    <div class="date">{{item.createTime | dateformat("YYYY-MM-DD HH:mm:ss")}}</div>
                </div>
            </div>
            <div class="content">{{item.content}}</div>
            <div class="control">
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <v-icon class="iconfont" :color="item.isLike ? 'blue lighten-2':''">mdi-thumb-up-outline</v-icon>
          <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
        </span>
                <span class="comment-reply" @click="showCommentInput(item)">
                    <v-icon class="iconfont">mdi-comment-processing-outline</v-icon>
                    <span>回复</span>
        </span>
            </div>
            <div class="reply">
                <div class="item" v-for="reply in item.reply">
                    <div class="reply-content">
                        <span class="from-name">{{reply.reviewerName}}</span><span>: </span>
                        <span class="to-name">@{{reply.commentatorName}}</span>
                        <span>{{reply.content}}</span>
                    </div>
                    <div class="reply-bottom">
                        <span>{{reply.createTime | dateformat("YYYY-MM-DD HH:mm:ss")}}</span>
                        <span class="reply-text" @click="showCommentInput(item, reply)">
                        <v-icon class="iconfont">mdi-comment-processing-outline</v-icon>
                        <span>回复</span>
            </span>
                    </div>
                </div>
                <div class="write-reply" v-if="item.reply.length > 0" @click="showCommentInput(item)">
                    <i class="el-icon-edit iconfont"/>
                    <span class="add-comment">添加新评论</span>
                </div>
                <transition name="fade">
                    <div class="input-wrapper" v-if="showItemId === item.id">
                        <el-input class="gray-bg-input"
                                  v-model="inputComment"
                                  type="textarea"
                                  :rows="3"
                                  autofocus
                                  placeholder="写下你的评论">
                        </el-input>
                        <div class="btn-control">
                            <span class="cancel" @click="cancel">取消</span>
                            <el-button class="btn" type="success" round @click="commitComment(item)">确定
                            </el-button>
                        </div>
                    </div>
                </transition>
            </div>
        </div>
    </div>
</template>

<script>
    import "@/assets/css/business/comment/comment.scss";


    export default {
        name: "comment",
        props: {
            comments: {
                type: Array,
                required: true
            }
        },
        components: {},
        data: () => ({
            inputComment: '',
            showItemId: '',
        }),
        computed: {},
        methods: {
            ilikeIt(commentId, like) {
                this.$axios.post("/api/comment/likeOrNot", this.$qs.stringify({
                    like: like,
                    commentId: commentId
                })).then(res => {
                    let resultData = res.data;
                    if (resultData && resultData.code === 200) {

                    }
                });
            },
            /**
             * 点赞
             */
            likeClick(item) {
                if (item.isLike === null) {
                    Vue.$set(item, "isLike", true);
                    this.ilikeIt(item.id, 1);
                    item.likeNum++;
                } else {
                    if (item.isLike) {
                        this.ilikeIt(item.id, 0);
                        item.likeNum--;
                    } else {
                        this.ilikeIt(item.id, 1);
                        item.likeNum++;
                    }
                    item.isLike = !item.isLike;
                }

            },
            /**
             * 清空回复界面
             */
            clearReplyArea() {
                this.inputComment = "";
                this.showItemId = "";
            },
            /**
             * 点击取消按钮
             */
            cancel() {
                this.showItemId = ''
            },

            /**
             * 提交评论
             */
            commitComment(item) {
                let commentId = item.id;
                let commentatorId = item.reviewerId;
                let reviewerName = item.name;
                //回复评论
                this.$axios.post("/api/comment/reply", this.$qs.stringify({
                    commentId: commentId,
                    commentatorId: commentatorId,
                    content: this.inputComment.replace("@" + reviewerName, "")
                })).then(res => {
                    let resultData = res.data;
                    if (resultData.code === 200) {
                        let resultMessage = resultData.message;
                        this.clearReplyArea();
                        this.$iziToast.success({message: resultMessage});
                        this.$emit("getCommentDataByBlogId");
                    }
                });

            },

            /**
             * 点击评论按钮显示输入框
             * item: 当前大评论
             * reply: 当前回复的评论
             */
            showCommentInput(item, reply) {
                if (reply) {
                    this.inputComment = "@" + reply.reviewerName + " "
                } else {
                    this.inputComment = ''
                }
                this.showItemId = item.id
            }
        },
        created() {
        }
    }
</script>

<style scoped>

</style>